<template>
  <el-drawer size="80%" :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="系统名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入系统名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="系统分类" prop="category">
            <el-radio-group v-model="formData.category">
              <el-radio
                v-for="dict in getIntDictOptions(DICT_TYPE.AGENT_SYSTEM_CATEGORY)"
                :key="dict.value"
                :label="dict.value"
              >
                {{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="系统描述" prop="description">
        <el-input
          v-model="formData.description"
          type="textarea"
          :rows="3"
          placeholder="请输入系统描述"
        />
      </el-form-item>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="系统图标" prop="icon">
            <div style="display: flex; gap: 8px; align-items: center;">
              <el-input 
                v-model="formData.icon" 
                placeholder="请输入Font Awesome图标类名，如：fas fa-desktop" 
                style="flex: 1;"
              >
                <template #prepend>
                  <i :class="formData.icon" v-if="formData.icon" style="width: 16px; text-align: center;"></i>
                  <i class="fas fa-icons" v-else style="width: 16px; text-align: center;"></i>
                </template>
              </el-input>
              <el-button 
                type="primary" 
                link 
                @click="openFontAwesome"
                style="white-space: nowrap;"
              >
                <i class="fas fa-external-link-alt" style="margin-right: 4px;"></i>
                图标库
              </el-button>
            </div>
            <div style="margin-top: 4px; font-size: 12px; color: #909399;">
              <span>示例：fas fa-desktop, fas fa-server, fas fa-cloud 等</span>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="系统版本" prop="version">
            <el-input v-model="formData.version" placeholder="请输入系统版本" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="访问地址" prop="url">
        <el-input v-model="formData.url" placeholder="请输入访问地址" />
      </el-form-item>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="演示账号" prop="username">
            <el-input v-model="formData.username" placeholder="请输入演示账号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="演示密码" prop="password">
            <el-input v-model="formData.password" placeholder="请输入演示密码" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="系统状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio label="在线">在线</el-radio>
          <el-radio label="离线">离线</el-radio>
          <el-radio label="维护中">维护中</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="备注说明" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          :rows="3"
          placeholder="请输入备注说明"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { DemoApi, type DemoVO } from '@/api/agent/manage/demo'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
/** 演示系统 表单 */
defineOptions({ name: 'DemoForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  description: undefined,
  icon: 'ep:monitor', // 保持Element Plus图标格式
  url: undefined,
  username: undefined,
  password: undefined,
  status: '在线',
  category: '软件系统',
  version: undefined,
  remark: undefined
})
const formRules = reactive({
  name: [{ required: true, message: '系统名称不能为空', trigger: 'blur' }],
  category: [{ required: true, message: '系统分类不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DemoApi.getDemo(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DemoVO
    if (formType.value === 'create') {
      await DemoApi.createDemo(data)
      message.success(t('common.createSuccess'))
    } else {
      await DemoApi.updateDemo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    description: undefined,
    icon: 'ep:monitor', // 保持Element Plus图标格式
    url: undefined,
    username: undefined,
    password: undefined,
    status: '在线',
    category: '软件系统',
    version: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}

/** 打开Font Awesome图标库 */
const openFontAwesome = () => {
  window.open('https://fontawesome.dashgame.com/', '_blank')
}
</script>
